package com.zyd.shiro.business.controller.pak;

import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Assets;
import com.zyd.shiro.business.enums.AssetsType;
import com.zyd.shiro.business.service.PakAssetsService;
import com.zyd.shiro.business.vo.AssetsConditionVO;
import com.zyd.shiro.business.vo.UserConditionVO;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.persistence.beans.PakPurchase;
import com.zyd.shiro.persistence.beans.PakPurchaseLine;
import com.zyd.shiro.util.ResultUtil;
import com.zyd.shiro.utils.ExcelTool;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.zyd.shiro.persistence.beans.PakAssets;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;
import java.io.File;
import com.zyd.shiro.utils.FileUtils;
import com.zyd.shiro.utils.ExcelUtil;
import java.util.Date;
import java.util.Map;


@RestController
@RequestMapping("/assets")
@Api(description = "资产清单")
public class PakAssetsController {
    @Autowired
    private PakAssetsService assetsService;

    @PostMapping
    @ApiOperation(value="添加", produces="application/json")
    public ResponseVO insert(Assets assets) {
        return ResultUtil.success("添加成功！",assetsService.insert(assets));
    }

    @PutMapping
    @ApiOperation(value="修改", produces="application/json")
    public ResponseVO update(Assets assets) {
        assetsService.updateSelective(assets);
        return ResultUtil.success("修改成功！",null);
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value="主键删除", produces="application/json")
    public ResponseVO delete(@PathVariable(name="id") Long id){
        Assets assets = assetsService.getByPrimaryKey(id);
        assets.getPakAssets().setStatus(AssetsType.DELETE.getCode());
        assetsService.updateSelective(assets);
        //assetsService.removeByPrimaryKey(id);
        return ResultUtil.success("删除成功！",null);
    }

    @GetMapping
    @ApiOperation(value="分页查询", produces="application/json")
    public PageResult page(AssetsConditionVO vo) {
        PageInfo<Assets> pageInfo = assetsService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }

    @GetMapping(value = "/code/{code}")
    @ApiOperation(value="code查询", produces="application/json")
    public ResponseVO code(@PathVariable(name="code") String code) {
        Assets assets = assetsService.findByCode(code);
        return ResultUtil.success("查询成功！",assets);
    }

    @GetMapping(value = "/all")
    @ApiOperation(value="查询所有", produces="application/json")
    public ResponseVO list() {
        List<Assets> assetss = assetsService.listAll();
        return ResultUtil.success("查询成功！",assetss);

    }

    @PostMapping(value = "/import")
    @ApiOperation(value="批量导入", produces="application/json")
    public ResponseVO folderImport(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.indexOf("."), fileName.length());
        if (suffix.equals(".xlsx") || suffix.equals(".xls")) {
            File newFile = new File(FileUtils.createFilePath(fileName));
            file.transferTo(newFile);

            List<Map<String, String>> list = ExcelTool.listData(newFile, PakAssets.class);
            if(list.size()==0){
                return ResultUtil.error("信息缺失!");
            }
            ExcelUtil<PakAssets> excelUtil = new ExcelUtil<>();
            List<PakAssets> pakAssetsList = excelUtil.dispatch(assetsService.convMapData(list),PakAssets.class);
            List<Assets> assets = new ArrayList<>();
            for(PakAssets pakAssets:pakAssetsList){
                assets.add(new Assets(pakAssets));
            }
            assetsService.insertList(assets);
            return ResultUtil.success("上传成功！");
        }
        return ResultUtil.error("请输入excel文件!");
    }
    @GetMapping(value = "/template")
    @ApiOperation(value="导入模板", produces="application/json")
    public ResponseVO template() {
        return ResultUtil.success("查询成功！","/upload/assets.xlsx");
    }

    @GetMapping(value = "/user")
    @ApiOperation(value="用户资产列表", produces="application/json")
    public PageResult userList(AssetsConditionVO assetsConditionVO) {
        PageInfo<Assets> pageInfo = assetsService.listUser(assetsConditionVO);
        return ResultUtil.tablePage(pageInfo);
    }

    @GetMapping(value = "/user/{id}/count")
    @ApiOperation(value="用户资产统计", produces="application/json")
    public ResponseVO count(@PathVariable(name="id") Long id) {
        List<Map> result = assetsService.groupStatus(id);


        return ResultUtil.success("请求成功！",result);
    }


}